package eerbaar.v2.ctr.ui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.util.Date;
import java.util.Random;

import eerbaar.area.IArea;
import eerbaar.client.internal.ctr.operator.area.IValue;
import eerbaar.client.internal.ctr.operator.area.ValueArea;
import eerbaar.client.internal.ctr.operator.area.ValueAreaHelper;
import eerbaar.elements.IElement;
import eerbaar.v2.app.tasks.IGameTask;

public class CtrUITask implements IGameTask {

    @Override
    public void doTask(Graphics2D g) {

        // MainWindow window = App.instance().getMainWindow();

        // Canvas canvas = window.getCanvas();
        // Graphics2D g = (Graphics2D) canvas.getBufferStrategy()
        // .getDrawGraphics();

        ValueArea valueArea = ValueAreaHelper.getValueArea();
        if (valueArea == null) {
            return;
        }

        Composite save = g.getComposite();

        int type = AlphaComposite.SRC_OVER;
        AlphaComposite alpha = AlphaComposite.getInstance(type, 0.1f);
        g.setComposite(alpha);

        Random r = new Random(new Date().getTime());

        IArea area = valueArea.getValueArea();
        IElement[] elements = area.getElements();

        double max = 0;
        double min = 0;
        for (IElement e : elements) {

            IValue value = e.getExtension(IValue.class);
            final double d = value.getValue();
            if (Math.abs(d) >= 500) {
                continue;
            }

            if (d > max) {
                max = d;
            }
            if (d < min) {
                min = d;
            }
        }

        double delta = 0;
        if (max > Math.abs(min)) {
            delta = max;
        } else {
            delta = Math.abs(min);
        }

        for (IElement e : elements) {

            final int x = (e.getPosition().getX() * 50);
            final int y = (e.getPosition().getY() * 50);

            IValue value = e.getExtension(IValue.class);
            final double d = value.getValue();

            if (Math.abs(d) >= 500) {
                continue;
            }
            if (d == 0) {
                continue;
            }

            double normal = d / delta;
            draw(g, r, x, y, normal);
            // System.out.println(normal);
        }

        g.setComposite(save);
    }

    private void draw(Graphics2D g, Random r, int x, int y, double d) {

        if (d > 0) {
            g.setColor(Color.BLACK);
        } else {
            g.setColor(Color.RED);
        }

        double dm = Math.abs(d);
        double width = dm;

        double w = width * 50d;
        if (w > 50) {
            w = 25;
        }
        if (w < 2) {
            w = 2;
        }
        double w12 = w / 2d;

        double x1 = (x + 25d) - w12;
        double y1 = (y + 25d) - w12;
        g.fillOval((int) x1, (int) y1, (int) w, (int) w);
    }

    @Override
    public Priority getPriority() {
        return Priority.MIDDLE;
    }

    @Override
    public int getRate() {
        return 1;
    }

    @Override
    public boolean runWhenSuspended() {
        return false;
    }

}
